package cn.stylefeng.guns.modular.business.frequency.orderlogistics.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.AndaPageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.frequency.cmcity.mapper.CmCityMapper;
import cn.stylefeng.guns.modular.business.frequency.orderlogistics.entity.OrderLogistics;
import cn.stylefeng.guns.modular.business.frequency.orderlogistics.mapper.OrderLogisticsMapper;
import cn.stylefeng.guns.modular.business.frequency.orderlogistics.model.param.ErrorMsg;
import cn.stylefeng.guns.modular.business.frequency.orderlogistics.model.result.TransportPageVO;
import cn.stylefeng.guns.modular.business.frequency.orderlogistics.service.OrderLogisticsService;
import cn.stylefeng.guns.modular.business.frequency.transport.mapper.TransportMapper;
import cn.stylefeng.guns.modular.business.frequency.transport.model.param.TransportDTO;
import cn.stylefeng.guns.modular.business.frequency.transportlevel.mapper.TransportLevelMapper;
import cn.stylefeng.guns.sys.modular.util.SnowIdUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 运单物流信息 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderLogisticsServiceImpl extends ServiceImpl<OrderLogisticsMapper, OrderLogistics> implements OrderLogisticsService {

    @Resource
    private TransportMapper transportMapper;
    @Resource
    private TransportLevelMapper transportLevelMapper;

    @Resource
    private CmCityMapper cmCityMapper;

    @Override
    public ResponseData getLogisticsList(TransportDTO orderDTO) {
        Page<TransportPageVO> page = baseMapper.getWaitLogisticsList(PageFactory.defaultPage(), orderDTO);
        if (page != null && page.getRecords().size() > 0) {
            for (TransportPageVO pkgTransport : page.getRecords()) {
                OrderLogistics orderLogistics = baseMapper.getLastLogistics(pkgTransport.getId());
                if (orderLogistics != null) {
                    pkgTransport.setLastLogisticsTime(orderLogistics.getCreateTime());
                    pkgTransport.setLastLogisticsAddress(orderLogistics.getArriveLocation());
                    //todo 创建用户姓名
                    //pkgTransport.setLastLogisticsUserName(orderLogistics.getCreateUserName());
                    pkgTransport.setLastLogisticsRemark(orderLogistics.getRemark());
                }

                String transportLine = pkgTransport.getLine();
                if (StringUtils.isNotBlank(transportLine)) {
                    String[] split = transportLine.split(",");
                    pkgTransport.setStartCodeName(cmCityMapper.getNameByCode(split[0]));
                    if(split.length>1){
                        pkgTransport.setDestinationCodeName(cmCityMapper.getNameByCode(split[split.length-1]));
                    }else {
                        pkgTransport.setDestinationCodeName("直达用户");
                    }

                }

            }
        }
        return ResponseData.andaSuccess(new AndaPageResult<>().AndaPageResult(page, orderDTO));
    }

    @Override
    public Page<TransportPageVO> getWaitLogisticsList(TransportDTO orderDTO) {
        Page<TransportPageVO> list = baseMapper.getWaitLogisticsList(PageFactory.defaultPage(), orderDTO);
        if (list != null && list.getRecords().size() > 0) {
            for (TransportPageVO pkgTransport : list.getRecords()) {
                OrderLogistics orderLogistics = baseMapper.getLastLogistics(pkgTransport.getId());
                if (orderLogistics != null) {
                    pkgTransport.setLastLogisticsTime(orderLogistics.getCreateTime());
                    pkgTransport.setLastLogisticsAddress(orderLogistics.getArriveLocation());
                    //todo 创建用户姓名
                    //pkgTransport.setLastLogisticsUserName(orderLogistics.getCreateUserName());
                    pkgTransport.setLastLogisticsRemark(orderLogistics.getRemark());
                }
            }
        }
        return list;
    }

    @Override
    public boolean checkLogisticsAdd(OrderLogistics orderLogistics, ErrorMsg errorMsg) {
        if (StringUtils.isBlank(orderLogistics.getArriveLocation())) {
            errorMsg.setErrorMsg("参数错误【缺少运单ID】");
            return false;
        }
        return true;
    }

    @Override
    public void addLogistics(OrderLogistics orderLogistics) {
        orderLogistics.setId(SnowIdUtils.uniqueLong());
        //orderLogistics.setCreateUser(SecurityUtil.getCurrentUserId(1));
        orderLogistics.setType(3);
        baseMapper.insert(orderLogistics);
        // 运单物流跟踪状态修改
        transportMapper.updateLogisticsStatus(orderLogistics.getTransportId(), 1);
    }


    public List<OrderLogistics> getLogisticsListByTransportId(Long transportId) {
        return baseMapper.getLogisticsListByTransportId(transportId);
    }

    public void confirmArrived(String levelId) {
        transportLevelMapper.updateArrivedStatus(Long.valueOf(levelId));
    }



}